﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Net.Mail;
using System.Configuration;

namespace KF.Common
{
    public class LogHelper
    {
        public static class ExceptionHelper
        {
            public enum ExceptionLevel
            {
                Infomation,     //普通信息记录
                Warning,        //警告，批量执行中，部分失败 或者 程序失败但是不影响业务和程序继续执行                
                Exception,      //程序执行发生异常，信息同步失败，但是没有造成数据错误
                Error           //程序执行发生严重问题，信息同步失败，而且可能已经产生错误数据
            }

           
        }

        public static class DebugHelper
        {
            private static object sync = new object();

            public static void WriteLog(string log, ExceptionHelper.ExceptionLevel eLevel)
            {
                StringBuilder filePath = new StringBuilder();
                StringBuilder logContent = new StringBuilder();
                
                try
                {
                    Monitor.Enter(sync);
                    DateTime curDatetime = DateTime.Now;

                    if (AppDomain.CurrentDomain.BaseDirectory == System.Environment.CurrentDirectory + "\\")
                    {
                        filePath.Append(Directory.GetCurrentDirectory());
                    }
                    else
                    {
                        try
                        {
                            filePath.Append(System.Web.HttpContext.Current.Server.MapPath(string.Empty));
                        }
                        catch
                        {
                            filePath.Append(AppDomain.CurrentDomain.BaseDirectory);
                        }
                    }
                    filePath.Append(@"\Logs\" + eLevel.ToString() + @"\");
                    filePath.Append(curDatetime.ToString("yyyyMM") + @"\");
                    if (!Directory.Exists(filePath.ToString()))
                    {
                        Directory.CreateDirectory(filePath.ToString());
                    }

                    logContent.AppendLine("######################################");
                    logContent.AppendLine(curDatetime.ToString("yyyy-MM-dd HH:mm:ss"));
                    logContent.AppendLine(log);
                    logContent.AppendLine("######################################");

                    using (StreamWriter sw = new StreamWriter(filePath.ToString() + curDatetime.ToString("dd") + ".txt", true))
                    {
                        sw.Write(logContent.ToString());
                        sw.Flush();
                        sw.Close();
                    }
                }
                catch
                {
                }
                finally
                {
                    Monitor.Exit(sync);
                }
            }

         

        }
    }
}
